<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	}

	function index()
	{
		// $this->load->view('upload_form', array('error' => ' ' ));
	}

	function do_upload_avatar()
	{
		//init upload
		$config['upload_path'] 		= './uploads/avatars/';
		$config['allowed_types'] 	= 'gif|jpg|png|jpeg';
		$config['overwrite'] 		= 'TRUE';
		$config['max_size']			= '2048';
		$config['max_width']  		= '4000';
		$config['max_height']  		= '4000';

		$ext = end(explode(".", $_FILES['userfile']['name']));
		// $config['file_name'] = 'myfile.'.$ext;

		$this->upload->initialize($config);

		if (!$this->upload->do_upload())
		{
			$error = $this->upload->display_errors();
			parent::print_result(array('upload' => $error));
		}
		else
		{
			$data = $this->upload->data();
			parent::print_result(array('full_path' => UPLOAD_FOLDER_AVATARS . $data['raw_name'] . '.' . $ext), SUCCESS_VALUE);
		}

		exit();
		// $success = file_put_contents($config['upload_path'] . "/afile", "This is a test");

		// if($success === false) {
		//     echo "Couldn't write file";
		// } else {
		//     echo "Wrote $success bytes";
		// }

		// $target_path = './uploads/avatar/' . basename($_FILES['userfile']['name']);
		// if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) 
		// {
		//     echo "The file ".  basename( $_FILES['userfile']['name']).
		//  " has been uploaded";
		// } 
		// else
		// {
		//     echo "There was an error uploading the file, please try again!";
		// }
	}

	function do_upload_product(){
		// init upload
		$config['upload_path'] 		= './uploads/products';
		$config['allowed_types'] 	= 'gif|jpg|png|jpeg';
		$config['overwrite']		= 'TRUE';
		$config['max_size']			= '2048';
		$config['max_width']  		= '4000';
		$config['max_height']  		= '4000';


		$ext = end(explode(".", $_FILES['userfile']['name']));
		$this->upload->initialize($config);

		if (!$this->upload->do_upload())
		{
			$error = $this->upload->display_errors();
			parent::print_result(array('upload' => $error));
		}
		else
		{
			$data = $this->upload->data();
			parent::print_result(array('full_path' => UPLOAD_FOLDER_PRODUCTS . $data['raw_name']. '.' . $ext), SUCCESS_VALUE);

			// update image product
		}
		exit();
	}
}
?>